package com.dcjt.firehotcarpad.manager;

import android.database.sqlite.SQLiteDatabase;

import com.dcjt.firehotcarpad.base.App;
import com.dcjt.firehotcarpad.entity.AppConfig;
import com.dcjt.firehotcarpad.entity.CarEntity;
import com.dcjt.firehotcarpad.entity.ConfigEntity;
import com.dcjt.firehotcarpad.entity.ConfigJCJEntity;
import com.dcjt.firehotcarpad.entity.DictEntity;
import com.dcjt.firehotcarpad.entity.PmqBean;
import com.dcjt.firehotcarpad.entity.SearchHistoryEntity;
import com.dcjt.firehotcarpad.entity.UserEntity;
import com.dcjt.firehotcarpad.entity.UsersInfoEntity;
import com.dcjt.firehotcarpad.greendao.CarEntityDao;
import com.dcjt.firehotcarpad.greendao.DaoMaster;
import com.dcjt.firehotcarpad.greendao.DaoSession;
import com.dcjt.firehotcarpad.greendao.DictEntityDao;
import com.dcjt.firehotcarpad.greendao.SearchHistoryEntityDao;
import com.dcjt.firehotcarpad.greendao.UserEntityDao;
import com.dcjt.firehotcarpad.greendao.UsersInfoEntityDao;

import java.util.List;

/**
 * Created by yangru on 2017/12/7.
 */

public class DataManager {
    private static DataManager mInstance;
    private static UserEntity mUser = null;
    private static AppConfig appConfigEntity = null;
    private static PmqBean pmqBean = null;
    private static String gpsbh = "";
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private DaoMaster.DevOpenHelper devOpenHelper;

    public DataManager() {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(App.get(),"DB_NAME_hot");
        SQLiteDatabase database = devOpenHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(database);
        mDaoSession = mDaoMaster.newSession();

    }

    public static DataManager getInstance() {
        if (mInstance == null) {
            mInstance = new DataManager();
        }
        return mInstance;
    }

    public DaoMaster getMaster() {
        return mDaoMaster;
    }

    public DaoSession getSession() {
        return mDaoSession;
    }

    public DaoMaster.DevOpenHelper getDevOpenHelper() {
        return devOpenHelper;
    }

    public DaoSession getNewSession() {
        if (mDaoSession == null) {
            mDaoSession = mDaoMaster.newSession();
        }
        return mDaoSession;
    }


    public  PmqBean getPmqBean() {
        return this.pmqBean;
    }

    public  void setPmqBean(PmqBean pmqBean) {
        this.pmqBean = pmqBean;
    }
    public  AppConfig getAppConfigEntity() {
        return this.appConfigEntity;
    }

    public  void setAppConfigEntity(AppConfig appConfig) {
        this.appConfigEntity = appConfig;
    }

    public String getGpsbh() {
        return this.gpsbh;
    }

    public void setGpsbh(String gpsbh) {
       this.gpsbh = gpsbh;
    }

    /**
     * 获取字典信息
     */
    public List<DictEntity> getDictInfo(String code) {
        DictEntityDao dictDao = mDaoSession.getDictEntityDao();
        List<DictEntity>  entities = dictDao.queryBuilder().where(DictEntityDao.Properties.Code.eq(code)).list();

       return entities;
    }
    /**
     * 获取指定字典信息
     */
    public DictEntity getDictInfo(String code,String dict_key ) {
        DictEntityDao dictDao = mDaoSession.getDictEntityDao();
        DictEntity  entities = dictDao.queryBuilder().where(DictEntityDao.Properties.Dict_key.eq(dict_key) ).unique();

       return entities;
    }
    /**
     * 保存搜索历史记录
     */
    public void saveSearchHistroy(SearchHistoryEntity entity) {
        SearchHistoryEntityDao searchDao = mDaoSession.getSearchHistoryEntityDao();
        List<SearchHistoryEntity>  entities = searchDao.queryBuilder().where(SearchHistoryEntityDao.Properties.Ajlx.eq(entity.getAjlx()),
                SearchHistoryEntityDao.Properties.Ajdj.eq(entity.getAjdj()),
                SearchHistoryEntityDao.Properties.Keyword.eq(entity.getKeyword())).list();

        if (entities.size()==0){
            List<SearchHistoryEntity> allEntity = searchDao.loadAll();
            searchDao.insert(entity);
            if (allEntity.size() == 15){
                searchDao.delete(allEntity.get(0));
            }
        }
    }
      /**
     * 获取搜索历史记录
     */
    public  List<SearchHistoryEntity>  getSearchHistroy() {
        SearchHistoryEntityDao searchDao = mDaoSession.getSearchHistoryEntityDao();
        List<SearchHistoryEntity>  entities = searchDao.loadAll();
        return entities;
    }

    /**
     * 保存字典信息
     */
    public void saveDictInfo(List<DictEntity> dict) {
        DictEntityDao dictDao = mDaoSession.getDictEntityDao();
        List<DictEntity>  entities = dictDao.queryBuilder().where(DictEntityDao.Properties.Code.eq(dict.get(0).getCode())).list();
//        List<DictEntity> entities = dictDao.queryRaw("code", dict.get(0).getCode());
        if (entities.size()>0){
            dictDao.queryBuilder().where(DictEntityDao.Properties.Code.eq(dict.get(0).getCode())) .buildDelete().executeDeleteWithoutDetachingEntities();
        }
        dictDao.insertInTx(dict);
    }
    /**
     * 保存车辆信息
     */
    public void saveCarInfo(List<CarEntity> dict) {
        CarEntityDao userDao = mDaoSession.getCarEntityDao();
        List<CarEntity> datas = userDao.loadAll();
        if (datas.size() > 0) {
            userDao.deleteAll();
        }
        userDao.insertInTx(dict);
    }
    /**
     * 获取车辆信息
     */
    public CarEntity getCarInfo(String account) {
        CarEntityDao dictDao = mDaoSession.getCarEntityDao();
        CarEntity  entities = dictDao.queryBuilder().where(CarEntityDao.Properties.Account.eq(account)).unique();

        return entities;
    }
    /**
     * 获取车辆信息
     */
    public CarEntity getCarInfoForCarName(String carName) {
        CarEntityDao dictDao = mDaoSession.getCarEntityDao();
        CarEntity  entities = dictDao.queryBuilder().where(CarEntityDao.Properties.Cph.eq(carName)).unique();

        return entities;
    }
    /**
     * 保存个人信息
     */
    public void saveUser(UserEntity user) {
        UserEntityDao userDao = mDaoSession.getUserEntityDao();
        List<UserEntity> datas = userDao.loadAll();
        if (datas.size() > 0) {
            userDao.deleteAll();
        }
        this.mUser = user;
        userDao.insert(user);
    }

    /**
     * 获取个人信息
     */
    public UserEntity getUser() {
        if (this.mUser == null) {
            UserEntityDao userDao = mDaoSession.getUserEntityDao();
            List<UserEntity> datas = userDao.loadAll();
            if (datas.size() > 0) {
                this.mUser = datas.get(0);
            }
        }

        return this.mUser;
    }
    /**
     * 保存相关消息人员信息
     */
    public void saveMoreUserInfo(List<UsersInfoEntity> list) {
        UsersInfoEntityDao userDao = mDaoSession.getUsersInfoEntityDao();
        List<UsersInfoEntity> datas = userDao.loadAll();
        if (datas.size() > 0) {
            userDao.deleteAll();
        }
        userDao.insertInTx(list);
    }


//    public void setUserTodayClock(int netData) {
//        ClockDetailDao clockDao = mDaoSession.getClockDetailDao();
//        clockDao.deleteAll();
//        if (netData >= 0) {
//            clockDao.insert(new ClockDetail(TimeUtils.getTodayFromSlash(), netData));
//        }
//    }
}
